package com.example.bsy.pinluosong;

import android.util.Log;

/**
 * Created by bsy on 2017/3/26.
 */
/*一张牌类*/
public class Card implements ICard{
    private int suit;
    private int point;
    private int color;
    boolean used;
    static Card[] fromCommand(String s,int start,int end){
        Card[] card=new Card[(end-start+1)/2];
        int i=0, pt=start;
        int suit,point;
        while(pt<end){
            suit=Integer.parseInt(s.charAt(pt)+"");
            pt++;
            //Log.i("s",s);
            if(s.charAt(pt)=='T'){
                point=10;
            }else if(s.charAt(pt)=='A'){
                point=1;
            }else if(s.charAt(pt)=='J'){
                point=11;
            }else if(s.charAt(pt)=='Q'){
                point=12;
            }else if(s.charAt(pt)=='K'){
                point=13;
            }else {
                point=Integer.parseInt(s.charAt(pt)+"");
            }
            pt++;
            card[i]=new Card(suit,point);
            i++;
        }
        return card;
    }
    static void exchange(Card a, Card b){
        Card t=new Card(a.getSuit(),a.getPoint());
        a.setSuit(b.getSuit());
        a.setPoint(b.getPoint());
        b.setSuit(t.getSuit());
        b.setPoint(t.getPoint());
    }

    //构造方法，给花色和点数赋值
    Card(){
        suit=1;
        point=1;
        repair();
    }
    Card(int p){
        suit=1;
        point=p;
        repair();
    }
    Card(int s, int p){
        suit=s;
        point=p;
        repair();
    }


    //set方法
    void setSuit(int s){
        suit=s;
        repair();
    }
    void setPoint(int p){
        point=p;
        repair();
    }
    void set(int s, int p){
        suit=s;
        point=p;
        repair();
    }

    //get方法
    @Override
    public int getSuit(){
       return suit;
    }
    @Override
    public int getPoint(){
        return point;
    }
    int getColor(){
        return color;
    }

    @Override
    public String toString(){
        String sSuit=null;
        String sPoint=Integer.toString(point);
        switch (suit){
            case 1:
                sSuit="♠";
                break;
            case 2:
                sSuit="♥";
                break;
            case 3:
                sSuit="♣";
                break;
            case 4:
                sSuit="♦";
                break;
        }
        switch (point){
            case 1:case 14:
                sPoint="A";
                break;
            case 11:
                sPoint="J";
                break;
            case 12:
                sPoint="Q";
                break;
            case 13:
                sPoint="K";
                break;

        }
        return sSuit+sPoint;

    }

    public String toCommand(){
        String sSuit=null;
        String sPoint=Integer.toString(point);
        switch (suit){
            case 1:
                sSuit="1";
                break;
            case 2:
                sSuit="2";
                break;
            case 3:
                sSuit="3";
                break;
            case 4:
                sSuit="4";
                break;
        }
        switch (point){
            case 1:case 14:
                sPoint="A";
                break;
            case 10:
                sPoint="T";
                break;
            case 11:
                sPoint="J";
                break;
            case 12:
                sPoint="Q";
                break;
            case 13:
                sPoint="K";
                break;

        }
        return sSuit+sPoint;
    }
    @Override
    public boolean isEqual(ICard c){
        if(suit==c.getSuit() && point==c.getPoint()) {
            return true;
        }else {
            return false;
        }
    }


    private void repair(){
        if(point<1 || point>14){
            point=1;
        }
        if(suit<1 || suit>4){
            suit=1;
        }
        switch (suit){
            case 1:
                color=0xff000000;
                break;
            case 2:
                color=0xffff0000;
                break;
            case 3:
                color=0xff0000ff;
                break;
            case 4:
                color=0xffff00ff;
                break;
        }
    }
}
